// Copyright 2019 The rhyme_lph Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// L [author] rhyme_lph
// N [email]  rhymelph@gmail.com
// T [github] https://github.com/rhymelph

import 'package:base_plugin/base_plugin.dart';
import 'package:finance_app/generated/a.dart';
import 'package:finance_app/generated/l10n.dart';
import 'package:finance_app/src/common/app_colors.dart';
import 'package:finance_app/src/common/app_spaces.dart';
import 'package:finance_app/src/provider/login/create_account_provider.dart';
import 'package:finance_app/src/widgets/button/normal_button.dart';
import 'package:finance_app/src/widgets/input/input_field.dart';
import 'package:finance_app/src/widgets/login/login_top_screen_widget.dart';
import 'package:flutter/material.dart';
import 'package:finance_app/src/router_gen/router_meta.dart';
import 'package:flutter_svg/svg.dart';

@RouterMeta(paramName: "createAccountPage")
class CreateAccountPage extends StatefulWidget {
  @override
  _CreateAccountPageState createState() => _CreateAccountPageState();
}

class _CreateAccountPageState extends State<CreateAccountPage>
    with PagePresenter<CreateAccountPage, CreateAccountProvider> {
  @override
  Widget get buildWidget => Scaffold(
        body: ListView(
          padding: EdgeInsets.zero,
          children: [
            _buildImageScreen(),
            listBuilder<bool>(
                builder: (BuildContext context, bool value, Widget child) =>
                    IndexedStack(
                      children: [
                        buildFirstScreen(),
                        buildSecondScreen(),
                      ],
                      index: value ? 1 : 0,
                    ),
                selector: (_, __) => __.showAuth)
          ],
        ),
      );

  Widget buildSecondScreen() => Column(
        children: [
          AppSpaces.h40,
          _buildTipText(
            tip: S.current.enable_biometric_login,
          ),
          AppSpaces.h40,
          SvgPicture.asset(
            A.assetsImagesLoginFingerprint_scanning_1,
            width: getDp(91),
            height: getDp(91),
            fit: BoxFit.contain,
          ),
          AppSpaces.h40,
          NormalButton(
            onTap: provider.onEnableFingerprint,
            leading: SvgPicture.asset(
              A.assetsImagesLoginFingerprintSmall,
              width: getDp(24),
              height: getDp(24),
              fit: BoxFit.contain,
            ),
            title: S.current.enable,
          ),
          AppSpaces.h20,
          PressedOpacityButton(
            onPressed: provider.onSkipFirst,
            child: Text(
              S.current.skip_first,
              style: TextStyle(
                color: aceBlueColor,
                fontSize: getSp(14),
                fontWeight: FontWeight.w700,
                height: 16 / 14,
              ),
            ),
          )
        ],
      );

  Widget buildFirstScreen() => Column(
        children: [
          AppSpaces.h35,
          _buildTipText(
            tip: S.current.let_s_set_up_your_account,
          ),
          AppSpaces.h20,
          InputField(
            key: ValueKey('create account full name'),
            leading: SvgPicture.asset(
              A.assetsImagesCreateAvatar1,
              width: getDp(15),
              height: getDp(15),
            ),
            tip: S.current.full_name,
            keyboardType: TextInputType.text,
            controller: provider.fullNameController,
            focusNode: provider.fullNameFocusNode,
          ),
          AppSpaces.h26,
          listBuilder<bool>(
              builder: (BuildContext context, bool value, Widget child) =>
                  InputField(
                    key: ValueKey('create account password'),
                    leading: SvgPicture.asset(
                      A.assetsImagesCreatePadlock2,
                      width: getDp(15),
                      height: getDp(15),
                    ),
                    tip: S.current.password,
                    // isPassword: true,
                    hasError: value,
                    controller: provider.passwordController,
                    focusNode: provider.passwordFocusNode,
                    helpText: S.current.at_least_6_alphanumerical_characters,
                  ),
              selector: (_, __) => __.passwordError),
          AppSpaces.h30,
          listBuilder<bool>(
              builder: (BuildContext context, bool value, Widget child) =>
                  NormalButton(
                    title: S.current.next,
                    onTap: value == true ? provider.onStartExploring : null,
                  ),
              selector: (_, __) => __.enable),
          AppSpaces.h26,
          buildStep(1),
        ],
      );

  Center buildStep(int index) {
    return Center(
      child: Text(
        S.current.step_index_out_of_3(index),
        style: TextStyle(
          color: aceDarkGrayColor,
          fontWeight: FontWeight.w400,
          fontSize: getSp(12),
        ),
      ),
    );
  }

  Widget _buildImageScreen() => LoginTopScreenWidget(
        imagePath: A.assetsImagesGraphicCreateProfile,
        imageName: S.current.create_account,
      );

  Center _buildTipText({String tip}) {
    return Center(
      child: Text(
        tip,
        style: TextStyle(
          color: aceBlackColor,
          fontSize: getSp(18),
          fontWeight: FontWeight.w700,
        ),
      ),
    );
  }
}
